/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simplemetrosystem.view.action;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import simplemetrosystem.view.MetroFrame;
import simplemetrosystem.view.MetroUpdate;

/**
 *
 * @author Eric Qian
 * 
 * 打开文件事件
 * 
 */
public class OpenFileActionListener implements ActionListener {

    public OpenFileActionListener(MetroFrame frame) {
        this.frame = frame;
    }

    public void actionPerformed(ActionEvent e) {
        openFile();
    }

    private void openFile() {
        JFileChooser chooser = new JFileChooser();
        MetroUpdate.updateFont(chooser, new Font(null, Font.PLAIN, 12));
        chooser.setCurrentDirectory(new File("."));

        chooser.setFileFilter(new FileFilter() {
            @Override
            public boolean accept(File f) {
                return f.isDirectory()||f.getName().toLowerCase().endsWith(".xml");
            }

            @Override
            public String getDescription() {
                return "XML files";
            }
        });
        
        int r = chooser.showOpenDialog(frame);
        if(r != JFileChooser.APPROVE_OPTION){
            return;
        }
        
        String path = chooser.getSelectedFile().getPath();
        
        frame.loadMap(path);
        System.out.println("OPEN ACTION:"+path);
    }
    private MetroFrame frame;
}
